package de.kimera.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import de.kimera.api.IEmail;
import de.kimera.controller.EmailManager;
import de.kimera.utils.Utils;
import javax.swing.JProgressBar;

public class SendEmailDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField fromTextField;
	private JPasswordField passwordField;
	private JTextField toTextField;
	private JTextField ccTextField;
	private JTextField subjectTextField;
	private JCheckBox savePasswordCheckBox;
	private JTextArea textArea;
	
	private IEmail email;
	private JProgressBar progressBar;
	
	/**
	 * Create the dialog.
	 */
	public SendEmailDialog(IEmail email) {
		setModalityType(ModalityType.APPLICATION_MODAL);
		setResizable(false);
		setTitle(Utils.LABEL_TITLE_SENDEMAILDIALOG);
		setBounds(Utils.calculateBounds(Utils.DIALOG_SENDEMAIL_WIDHT, Utils.DIALOG_SENDEMIL_HEIGHT));
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel lblFrom = new JLabel(Utils.LABEL_EMAIL_FROM);
			lblFrom.setBounds(5, 10, 70, 20);
			contentPanel.add(lblFrom);
		}
		{
			fromTextField = new JTextField();
			fromTextField.setBounds(80, 10, 390, 20);
			contentPanel.add(fromTextField);
			fromTextField.setColumns(10);
		}
		{
			JLabel lblPassword = new JLabel(Utils.LABEL_EMAIL_PASSWORD);
			lblPassword.setBounds(5, 40, 70, 20);
			contentPanel.add(lblPassword);
		}
		{
			passwordField = new JPasswordField();
			passwordField.setBounds(80, 40, 230, 20);
			contentPanel.add(passwordField);
		}
		{
			JLabel lblTo = new JLabel(Utils.LABEL_EMAIL_TO);
			lblTo.setBounds(5, 70, 70, 20);
			contentPanel.add(lblTo);
		}
		{
			toTextField = new JTextField();
			toTextField.setBounds(80, 70, 390, 20);
			contentPanel.add(toTextField);
			toTextField.setColumns(10);
		}
		{
			JLabel lblCc = new JLabel(Utils.LABEL_EMAIL_CC);
			lblCc.setBounds(5, 100, 70, 20);
			contentPanel.add(lblCc);
		}
		{
			ccTextField = new JTextField();
			ccTextField.setBounds(80, 100, 390, 20);
			contentPanel.add(ccTextField);
			ccTextField.setColumns(10);
		}
		{
			JLabel lblSubject = new JLabel(Utils.LABEL_EMAIL_SUBJECT);
			lblSubject.setBounds(5, 130, 70, 20);
			contentPanel.add(lblSubject);
		}
		{
			subjectTextField = new JTextField();
			subjectTextField.setBounds(80, 130, 390, 20);
			contentPanel.add(subjectTextField);
			subjectTextField.setColumns(10);
		}
		{
			JLabel lblText = new JLabel(Utils.LABEL_EMAIL_TEXT);
			lblText.setBounds(5, 160, 70, 20);
			contentPanel.add(lblText);
		}
		{
			textArea = new JTextArea();
			textArea.setLineWrap(true);
			textArea.setWrapStyleWord(true);
			textArea.setBounds(80, 160, 390, 150);
			contentPanel.add(textArea);
		}
		
		savePasswordCheckBox = new JCheckBox(Utils.LABEL_EMAIL_SAVEPASSWORD);
		savePasswordCheckBox.setBounds(330, 38, 140, 23);
		contentPanel.add(savePasswordCheckBox);
		
		progressBar = new JProgressBar();
		progressBar.setStringPainted(true);
		progressBar.setBounds(80, 321, 390, 14);
		progressBar.setVisible(false);
		contentPanel.add(progressBar);
		
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton(Utils.LABEL_BUTTON_SENDEMAIL);
				okButton.setName(Utils.LABEL_BUTTON_SENDEMAIL);
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						sendEmail();						
					}
				});
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton(Utils.LABEL_BUTTON_CANCEL);
				cancelButton.setName(Utils.LABEL_BUTTON_CANCEL);
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						dispose();
					}
				});
				buttonPane.add(cancelButton);
			}
		}
		
		showEmailData(email);
	}
	
	private void showEmailData(IEmail email){
		this.email = email;
		fromTextField.setText(email.getFrom());
		passwordField.setText(email.getPassword());
		toTextField.setText(email.getTo());
		ccTextField.setText(email.getCc());
		subjectTextField.setText(email.getSubject());
		savePasswordCheckBox.setSelected(email.isPasswordSaved());
		textArea.setText(email.getText());
	}
	
	public void sendEmail(){
		email.setFrom(fromTextField.getText());
		email.setPassword(new String(passwordField.getPassword()));
		email.setTo(toTextField.getText());
		email.setCc(ccTextField.getText());
		email.setSubject(subjectTextField.getText());
		email.setPasswordSaved(savePasswordCheckBox.isSelected());
		email.setText(textArea.getText());
			
		progressBar.setVisible(true);
		
		EmailManager emailManager = new EmailManager(this, progressBar,email);
		emailManager.start();
	}
}
